import type { OptionItem } from '@/types/common';
import { DIRECT_REASON_LIST } from '../constants/index';

export interface SelectorConfig<T> {
  onSelect: (type: keyof T, item: OptionItem) => void;
}

export function useAccCommon<T extends Record<string, any>>(config?: SelectorConfig<T>) {
  const directStore = useDirectStore();

  // 响应式数据
  const tranposList = ref<OptionItem[]>([]);
  const logistList = ref<OptionItem[]>([]);
  const userList = ref<OptionItem[]>([]);
  const reasonList = ref<OptionItem[]>([...DIRECT_REASON_LIST]);

  // 加载状态
  const loading = ref({
    address: false,
    logistics: false,
    user: false,
    reason: false,
  });

  // 选择器显示状态
  const showStates = reactive({
    address: false,
    logistics: false,
    user: false,
    reason: false,
  });

  // 模拟接口调用
  const fetchTranposList = async () => {
    loading.value.address = true;
    const response = await DirectService.queryAddress({
      customId: directStore.customerObj.customId,
      contactId: directStore.contactObj.contactId || '',
      entryId: directStore.customerObj.entryId,
    });
    tranposList.value = response.map((item: any) => ({
      id: item.tranposId,
      name: item.tranposName,
    }));
    loading.value.address = false;
  };

  const fetchLogistList = async (tranposId: number) => {
    loading.value.logistics = true;
    const response = await DirectService.queryLogisticsCenter({
      customId: directStore.customerObj.customId,
      contactId: directStore.contactObj.contactId || '',
      entryId: directStore.customerObj.entryId,
      tranposId,
    });
    logistList.value = response.map((item: any) => ({
      id: item.logistCenterId,
      name: item.logistCenterName,
    }));
    loading.value.logistics = false;
  };

  const fetchUserList = async (tranposId: number, logistCenterId: number) => {
    loading.value.user = true;
    const response = await DirectService.queryDirectrgMan({
      customId: directStore.customerObj.customId,
      contactId: directStore.contactObj.contactId || '',
      entryId: directStore.customerObj.entryId,
      tranposId,
      logistCenterId,
    });
    userList.value = response.map((item: string) => ({
      name: item,
    }));
    loading.value.user = false;
  };

  // 选择器处理函数
  const selectHandlers = {
    address: (item: OptionItem) => {
      config?.onSelect('address' as keyof T, item);
      showStates.address = false;
    },
    logistics: (item: OptionItem) => {
      config?.onSelect('logistics' as keyof T, item);
      showStates.logistics = false;
    },
    user: (item: OptionItem) => {
      config?.onSelect('user' as keyof T, item);
      showStates.user = false;
    },
    reason: (item: OptionItem) => {
      config?.onSelect('reason' as keyof T, item);
      showStates.reason = false;
    },
  };

  // 关闭处理函数
  const closeHandlers = {
    address: () => {
      showStates.address = false;
    },
    logistics: () => {
      showStates.logistics = false;
    },
    user: () => {
      showStates.user = false;
    },
    reason: () => {
      showStates.reason = false;
    },
  };

  return {
    // 响应式数据
    tranposList: readonly(tranposList),
    logistList: readonly(logistList),
    userList: readonly(userList),
    reasonList: readonly(reasonList),
    loading: readonly(loading),

    // 选择器相关
    showStates,
    selectHandlers,
    closeHandlers,

    // 方法
    fetchTranposList,
    fetchLogistList,
    fetchUserList,
  };
}
